﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using LoginWebApplication.DataSetTableAdapters;
using System.ServiceModel.Activation;

namespace LoginWebApplication
{
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "LoginService" in code, svc and config file together.
    public class LoginService : ILoginService
    {
        private ClientDataTableAdapter cdta = new ClientDataTableAdapter();
        private AdministratorDataTableAdapter adta = new AdministratorDataTableAdapter();
        User user;
        string password;
        public string Login(string loginName, string password, string userType)
        {
            this.password = password;
            if (userType == "Client")
            {
                var Table = cdta.GetUserByClientName(loginName);
                if (Table.Rows.Count > 0)
                {
                    user = new User()
                            {
                                id = Table[0].Id,
                                loginname = Table[0].ClientName,
                                password = Table[0].Password
                            };
                    return LoginSuccess(user, userType,user.id);
                }
            }
            else if (userType == "Admin")
            {
                var Table = adta.GetUserByAdministratorName(loginName);
                if (Table.Rows.Count > 0)
                {

                    user = new User()
                    {
                        id = Table[0].Id,
                        loginname = Table[0].AdministratorName,
                        password = Table[0].Password
                    };
                    return LoginSuccess(user, userType, user.id);
                }
            }

            return "Failed";

        }

        private string LoginSuccess(User user, string userType, int p)
        {
            if (password == user.password)
            {
                return "LoginSuccess, You are login as a " + userType + " and ID is " + p.ToString();
            }
            else { return "Failed"; };

        }




    }
}
